package com.woniuxy.credit_loans.collection_zhang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.credit_loans.collection_zhang.dto.PageDto;
import com.woniuxy.credit_loans.collection_zhang.service.CheckedService;
import com.woniuxy.credit_loans.collection_zhang.vo.CheckVo;
import com.woniuxy.credit_loans.entity.Borrow;
import com.woniuxy.credit_loans.entity.Checked;
import com.woniuxy.credit_loans.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/Checked")
public class CheckedController {

    @Autowired
    private CheckedService checkedService;

    @PostMapping("/page")
    private Result selectAll(@RequestBody PageDto pageDto){
        PageHelper.startPage(pageDto.getCurrentPage(),pageDto.getPagesize(),true);
        List<CheckVo> checkeds = checkedService.selectAll(pageDto.getInput());
        PageInfo<CheckVo> pageInfo = new PageInfo<CheckVo>(checkeds);

        return  Result.ok().data("list",checkeds).data("pageInfo",pageInfo.getTotal());
    }

    @PostMapping("")
    private  Result add(@RequestBody Checked checked){
         int i = checkedService.insertSelective(checked);
        if (i==0) {
            return  Result.ok();
        }else {
            return  Result.error();
        }
    }

    @GetMapping("{id}")
    private  Result selectById(@PathVariable Long id){
         Checked checked = checkedService.selectByPrimaryKey(id);
         return  Result.ok().data("checked",checked);

    }

    @PutMapping("")
    private  Result  update(@RequestBody Checked checked){
        System.out.println(checked);
         int i = checkedService.updateByPrimaryKeySelective(checked);
        if (i==0) {
            return  Result.ok();
        }else {
            return  Result.error();
        }
    }

    @DeleteMapping("{id}")
    private  Result delete(@PathVariable Long id){
         int i = checkedService.deleteByPrimaryKey(id);
        if (i==0) {
            return  Result.ok();
        }else {
            return  Result.error();
        }
    }

}
